<template>
  <div class="search-result">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.art_id" :title="item.title" />
    </van-list>
  </div>
</template>

<script>
import {getSearchResult} from '@/api/search'
export default {
  name: 'SearchResult',
  components: {},
  // 通过父子通信把输入框的值传给搜索结果
  props: {
    searchText:{
      type:String
    }
  },
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      page:1
    }
  },
  methods: {
   async onLoad () {
         try {
          //  1 发送请求获取数据(将数据追加给列表变量)
           let {data} =await getSearchResult({
             page:this.page, //页数
             per_page:10, //每页数量
             q:this.searchText //搜索关键词
           })
           this.list.push(...data.data.results)
              // 2 关闭loading 
              this.loading=false
              // 3 判断是否结束
              if(data.data.results.length < 10) {
                // 3.1 结束了 finished 为true 
                this.finished=true
              }else {
                // 3.2 未结束 页码要变化
                this.page++
              }
         } catch (error) {
           console.log(error);
         }
     
  }
  }
}
</script>

<style scoped lang="less">

</style>
